﻿using SafoverWpfFw;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using Safover.Lidar.DataContract;

namespace Safover.LidarCollector.Converter
{
    public class LidarWorkModeToVisibilityConverter : BaseValueConverter<LidarWorkModeToVisibilityConverter>
    {
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DoConvert(value,  parameter);
        }

        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        private object DoConvert(object value, object parameter) 
        {
            var visibility = Visibility.Collapsed;

            if (Enum.TryParse(value.ToString(), out LidarWorkMode current))
            {
                if (parameter != null)
                {
                    var providedEnums = ParseEnums(parameter);

                    visibility = providedEnums.Contains(current) ? Visibility.Visible : Visibility.Collapsed;
                }
                else
                {
                    if (current != LidarWorkMode.Multi)
                    {
                        visibility = Visibility.Visible;
                    }
                }
            }

            return visibility;
        }

        private HashSet<LidarWorkMode> ParseEnums(object parameter)
        {
            var providedEnums = new HashSet<LidarWorkMode>();

            var segments = parameter.ToString().Split('_');

            foreach (var segment in segments)
            {
                if (Enum.TryParse(segment.Trim(), out LidarWorkMode providedEnum))
                {
                    providedEnums.Add(providedEnum);
                }
            }

            return providedEnums;
        }
    }
}
